package com.hellloop.pim.alphabeat.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.hellloop.pim.alphabeat.entity.LivingRoomEntity;
import com.hellloop.pim.alphabeat.entity.RoomProperties;
import com.hellloop.pim.alphabeat.game.PIMClientGame;

public class AlphabeatUI extends JFrame {

	public static final String TITLE_NAME = "Alphabeat Prototype version 0.3";
	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;
	public static final int MIN_SCORE = 0;
	public static final int MAX_SCORE = 10000;

	private PIMClientGame pimClientGame;

	// main panel
	private JPanel loginPanel;
	private LivingRoom livinPanel;
	private BattleRoom fightPanel;

	// login panel
	private JLabel userLabel, passLabel;
	private JTextField userField;
	private JPasswordField passField;
	private JButton connButton, exitButton;

	// user information
	private String username;

	public AlphabeatUI(PIMClientGame pimClientGame) {
		this.pimClientGame = pimClientGame;
		InitComponents();
	}

	private void InitComponents() {
		this.setTitle(TITLE_NAME);
		this.setSize(WIDTH, HEIGHT);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// create main panel
		Container cp = this.getContentPane();
		cp.setLayout(new BorderLayout());

		// fight panel
		fightPanel = new BattleRoom(pimClientGame, this);

		// livin panel
		livinPanel = new LivingRoom(pimClientGame, this);

		// login panel
		loginPanel = new JPanel();

		userLabel = new JLabel("Username:");
		userLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		// passLabel = new JLabel("Password:");
		// passLabel.setHorizontalAlignment(SwingConstants.RIGHT);

		userField = new JTextField("", 10);
		userField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				processLogin();
			}
		});
		// passField = new JPasswordField("", 10);
		// passField.addActionListener(new ActionListener(){
		// public void actionPerformed(ActionEvent arg0) {
		// processLogin();
		// }
		// });

		JPanel buttonPanel = new JPanel();
		connButton = new JButton("Login");
		connButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				processLogin();
			}
		});
		exitButton = new JButton("Exit");
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		buttonPanel.setLayout(new GridLayout(1, 1, 4, 4));
		buttonPanel.add(connButton);
		buttonPanel.add(exitButton);

		loginPanel.setLayout(new GridBagLayout());
		GridBagConstraints gridBagConstraints = new GridBagConstraints();

		gridBagConstraints.insets = new Insets(4, 4, 4, 4);

		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		loginPanel.add(userLabel, gridBagConstraints);

		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = GridBagConstraints.NONE;
		loginPanel.add(userField, gridBagConstraints);

		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		// loginPanel.add(passLabel, gridBagConstraints);

		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = GridBagConstraints.NONE;
		// loginPanel.add(passField, gridBagConstraints);

		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.fill = GridBagConstraints.REMAINDER;
		loginPanel.add(new JLabel(), gridBagConstraints);

		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.fill = GridBagConstraints.REMAINDER;
		loginPanel.add(buttonPanel, gridBagConstraints);

		cp.add(loginPanel, BorderLayout.CENTER);

		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	private void processLogin() {
		username = userField.getText().trim();

		if (!username.equals("") && !username.equals(null)) {

			connButton.setEnabled(false);

			// send username and password to server
			pimClientGame.connect(username, "PleHenHen");
			// wait for result
			// if ok, then show battle room
			// if not, show error msg dialog
//			switchToLivingRoom();

			connButton.setEnabled(true);
		}

		// passField.setText("");
	}

	public void switchToLivingRoom(LivingRoomEntity livingRoomEntity, boolean isOwner) {
		getContentPane().add(livinPanel);
		livinPanel.setVisible(true);
		loginPanel.setVisible(false);
		fightPanel.setVisible(false);
		getContentPane().remove(loginPanel);
		getContentPane().remove(fightPanel);
	}

	public void switchToBattleRoom() {
		getContentPane().add(fightPanel);
		fightPanel.setVisible(true);
		loginPanel.setVisible(false);
		livinPanel.setVisible(false);
		getContentPane().remove(loginPanel);
		getContentPane().remove(livinPanel);
	}

	public void switchToLoginScreen() {
		getContentPane().add(loginPanel);
		loginPanel.setVisible(true);
		fightPanel.setVisible(false);
		livinPanel.setVisible(false);
		getContentPane().remove(fightPanel);
		getContentPane().remove(livinPanel);
	}

	public QuestionPanel getQuestionPanel() {
		return fightPanel.getQuestionPanel();
	}
}
